package com.ruleengine.executer.impl;

import org.apache.log4j.Logger;

import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ruleengine.executer.InstructionExecuter;
import com.ruleengine.executer.exception.InstructionExecuteException;
import com.ruleengine.instruction.impl.ConditionInstruction;
import com.wx.express.calculator.ExpressCalculator;

public class ConditionInstructionExecuter implements InstructionExecuter<ConditionInstruction>{
    private static Logger logger = Logger.getLogger(ConditionInstructionExecuter.class);
    
    public boolean execute(IWorkflowConext context, IEndpointData data,
            ConditionInstruction instruction) throws InstructionExecuteException {
        // TODO Auto-generated method stub
        try{
            //计算表达式值
            Boolean result = (Boolean)(ExpressCalculator.instance().eval(instruction.getExpression(),new Object[]{data}));
            if(!result){
                //表达式不成立
                // 跳过前面的offset个指令
                context.addPcCount(instruction.getGotoOffset());
            }
            return true;
        }catch(Exception e){
            throw new InstructionExecuteException(e);
        }
    }

}
